package com.example.yang_app_android_fifth_02;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    Context useCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Context useCount = null;
        try{
            // 获取其他程序所对应的Context
            useCount =createPackageContext( "com.example.yang_app_android_fifth" , Context.CONTEXT_IGNORE_SECURITY);
        }
        catch (NameNotFoundException e){
            e.printStackTrace();
        }
        // 使用其他程序的Context获取对应的SharedPreferences
        SharedPreferences prefs =getSharedPreferences ("count", Context.MODE_PRIVATE);
        // 读取数据
        int count = prefs.getInt ("count", 0);
        Toast.makeText(this , "程序以前被使用了" + count + "次。", Toast.LENGTH_LONG).show();
    }
}

